// address收货地址下拉框渲染
$(".ct-addrinfo .uitem-sel").click(e => {
    e = window.event;
    target = e.currentTarget;
    $(target.children[2]).toggle()
})
$(function () { //调用插件
    $.fn.citySelect();
});
$(function () {
    form1 = $('form[name=form1]'),
        prev = $('input[name=cho_Province]', form1),
        city = $('input[name=cho_City]', form1),
        area = $('input[name=cho_Area]', form1),
        value = ['请选择省份', '请选择城市', '请选择地区'];
    form1.submit(function () {
        if (prev.val() == value[0]) {
            alert(value[0]);
            return false;
        };
        if (city.val() == value[1]) {
            alert(value[1]);
            return false;
        };
        if (area.val() == value[2]) {
            alert(value[2]);
            return false;
        }
    });
});

// 正则验证
//手机号
$(document).on('keyup', '.sjh', function () {
    if ($(".sjh").val().match(/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/) == null) {
        $('uitem-ipt span.red').fadeIn();
        $('.sjh').addClass('error_input');
        return;
    } else {
        var Users = (localStorage.getItem('Users') == null || JSON.parse(localStorage.getItem('Users')).length == 0) ? new Array() : JSON.parse(localStorage.getItem('Users'));
        for (var i = 0; i < Users.length; i++) {
            if ([i].UserName == $(this).val()) {
                $('.sjh').addClass('error_input');
                return;
            }
        }
        $('uitem-ipt span.red').fadeOut();
        $('.sjh').removeClass('error_input');
    }
})

// 地址渲染，调用数据库

/**登录后操作**/

if (sessionStorage.uname) {
    $("#dz .center-none").hide();
    $("#dz .addr-head").show();
    $("#dz .addr-list").show();

    //新增地址
    $(".ct-userform .btn-uitem-save").click(() => {
        $("#addr").submit();
        return false;
    })

    // 传送用户名
    var somebody=sessionStorage.uname;
    var inp=document.createElement("input");
        inp.type="hidden";
        inp.name="uname";
        inp.value=somebody;
        addr.appendChild(inp);

    // 渲染 
    $.get("../php/jc_person.php", `hide=addr&uname=${somebody}`, data => {
        data.forEach(obj => {
            if(obj.uname==somebody){
                $(".addr-list>ul").append($(`<li>
                <span class='addrlist-name'>${obj.name}</span>
                <span class='addrlist-addr'>${obj.addr}</span>
                <span class='addrlist-tel'>${obj.phone}</span>
                <span class='addrlist-action'>
                    <a href='#' class='btn-del' onclick='del(this)'>删除</a>
                    <input type='hiden' value='${obj.id}' style='display:none'>
                </span>
                </li>`))
            }   
        })
    }, "JSON")

    function del(btn){
        var id=btn.nextElementSibling.value;
        $.get("../php/jc_person.php",`hide=delAddr&id=${id}`,data=>{
            if(data){
                btn.parentNode.parentNode.remove();
            }
        })
    }
}




